<template>
  <div @click="onChange" :class="`switch ${ on ? '' : 'off' }`">
    <div :class="`btn ${ on ? '' : 'off' }`"></div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      on: false,
    };
  },
  methods: {
    onChange () {
      this.on = !this.on;
    },
  },
};
</script>

<style scoped>
.switch {
  height: 100px;
  width: 200px;
  background: #1890ff;
  border-radius: 50px;
  padding: 8px;
  box-sizing: border-box;
}
.switch.off {
  background: #ccc;
}
/* 按钮在右侧的样式 */
.switch .btn {
  height: 84px;
  width: 84px;
  background: #fff;
  border-radius: 42px;
  float: right;
  transition: transform 0.3s ease, width 0.3s ease;
}
/* 按钮在左侧的样式 */
.switch .btn.off {
  transform: translateX(-100px);
}

/* 鼠标按下时的.switch .btn */
.switch:active .btn {
  width: 100px;
}

.switch:active .btn.off {
  transform: translateX(-84px);
}
</style>
